<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/WEB-INF/jsp/commons/taglibs.jsp" %>
<!DOCTYPE HTML>
<html ng-app>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <title>${title}</title>
    <%@ include file="/WEB-INF/jsp/commons/meta.jsp" %>
    <link href="${ctx}/resources/new/style.css" type="text/css" rel="stylesheet"/>

</head>
<body>
<style>
    #eyeInfo {
        display: float;
        float: left;
    }

    .barSpan {
        float: right;
        width: 80%;
    }

    .tab {
        margin-bottom: 0px;
        margin-top: 6px;
    }

    .tab > li > a {
        line-height: 22px;
        padding-bottom: 2px;
        padding-top: 2px;
    }

    .tab-content {
        border: 1px solid #E3E3E3;
        border-radius: 0px 0px 4px 4px;
        border-top: none;
        padding: 2px;
    }

    .table [valign="top"] {
        vertical-align: top;
    }

    #newOrder div[id^=m] {
        background-color: #FFFFFF;
        border: 1px solid #E3E3E3;
        border-radius: 4px;
        margin: 2px;
        padding: 2px;
        line-height: 25px;
        float: left;
        min-height: 30px;
        width: 385px;
    }

    .title {
        width: 60px;
        margin-left: 20px;
    }

    table {
        width: 100%;
    }
</style>
<ul class="nav nav-tabs tab tBlock">
    <li class="active">
        <a title="新增订单" deny="not" class="nFocus" href="#newOrder" data-toggle="tab">
            <i class="icon-heart"></i>
            我要下单
        </a>
    </li>
</ul>
<div class="tab-content">
    <div class="tab-pane active" id="newOrder">
        <form id="inputForm" class="form-inline" action="${ctx}/adoms/order/order" method="post">
            <input type="hidden" name="id" value="${order.id}"/>
            <table class="table table-striped table-bordered table-condensed">
                <tr>
                    <td class="span2">订单名称:</td>
                    <td>
                        <input id="name" class="span4" name="name" type="text" value="${order.name}" size="30"
                               maxlength="80"/>
                    </td>
                </tr>
                <tr>
                    <td class="span2">发货部门:</td>
                    <td>
                        <select id="departmentTarget" name="departmentTarget.id" style="width: auto; min-width: 200px;">
                            <option id="dPlz">请选择...</option>
                            <c:forEach items="${dList }" var="d" varStatus="index">
                                <option value="${d.id }">${index.index + 1}.${d.name }</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="span2">订购商品:</td>
                    <td>
                        <select id="marchandise" name="marchandise.id" style="width: auto; min-width: 200px;">
                            <option id="plz">请选择...</option>
                            <c:forEach items="${mList }" var="m" varStatus="index">
                                <option value="${m.id }">${index.index + 1}.${m.name }</option>
                            </c:forEach>
                        </select>
                        (可选择多个商品)
                    </td>
                </tr>
                <tr>
                    <td class="span2" valign="top">商品订购详情:</td>
                    <td>
                        <div id="eyeInfo">
                            <c:forEach items="${mList }" var="m" varStatus="index">
                                <div disable="disable" class="hide" id="m${m.id }">
                                    <table minfo class="table-striped table-bordered table-condensed"
                                           style="margin: auto;">
                                        <tr>
                                            <td colspan="2">
                                                商品名称：${index.index + 1}.${m.name }
                                                <input type="hidden" name="marchandise.id" value="${m.id }">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                单价：
                                                <input name="mPrice" value="${m.price }" type="text" readonly="readonly"
                                                       class="tRed span1">
                                                元
                                            </td>
                                            <td>
                                                规格：
                                                <span>${m.spcification }</span>
                                            </td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="2">
                                                数量：
                                                <input name="boxNum" value="0" type="text" class="tRed span1">
                                                (件) X
                                                <span name="numOfBox">${m.numOfBox }</span>
                                                    ${m.packageing } /件 +
                                                <input name="items" value="0" type="text" class="tRed span1">
                                                (${m.packageing })
                                            </td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                合计：
                                                <input name="totalPrice" type="text" value="0.0" readonly="readonly"
                                                       class="span1">
                                                (元)
                                            </td>
                                            <td>
                                                总数量：
                                                <input maxlength="6" readonly="readonly" style="width: 100px;" value="0"
                                                       name="orderNum" class="span2" type="text">
                                                (${m.packageing })
                                            </td>
                                        </tr>
                                    </table>
                                    <p class="barSpan">
										<span>
											<a class="exspan" to="#m${m.id }" title="查看详情" href="#">
                                                <i class="icon-eye-open"></i>
                                                查看详情
                                            </a>
										</span>
										<span>
											<a class="rclose" value="${m.id }" to="#m${m.id }" title="删除商品" href="#">
                                                <i class=" icon-remove"></i>
                                                删除商品
                                            </a>
                                            </sapn>
                                    </p>

                                    <div class="hide" style="display: inline-block; text-align: center;">
                                        <table class="table-striped table-bordered table-condensed"
                                               style="margin: auto; width: auto;">
                                            <tr>
                                                <td>商品名称：</td>
                                                <td>${index.index + 1}.${m.name }</td>
                                            </tr>
                                            <tr>
                                                <td>商品编号：</td>
                                                <td>${m.code }</td>
                                            </tr>
                                            <tr>
                                                <td>单价：</td>
                                                <td>
													<span class="tBlue">
														<span>${m.price }</span>
														元
													</span>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>商品条码：</td>
                                                <td>${m.barCode }</td>
                                            </tr>
                                            <tr>
                                                <td>品牌：</td>
                                                <td>${m.brand.name }</td>
                                            </tr>
                                            <tr>
                                                <td>产地：</td>
                                                <td>${m.producePlace }</td>
                                            </tr>
                                            <tr>
                                                <td>保质期：</td>
                                                <td>${m.shelflife }月</td>
                                            </tr>
                                            <tr>
                                                <td>商品描述：</td>
                                                <td>${m.description }</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            </c:forEach>
                        </div>
                    </td>
                </tr>
                <tr class="hide">
                    <td>商品单价:</td>
                    <td class="tBlue">
                        ￥
                        <span id="mPrice" class="tRed">0.00</span>
                        (元)
                    </td>
                </tr>
                <tr class="hide">
                    <td>订购总数:</td>
                    <td>
                        <input class="span1" id="goodsNumber" name="goodsNumber" readonly="readonly" type="text"
                               value="0" size="30" maxlength="50"/>
                    </td>
                </tr>
                <tr>
                    <td>总价:</td>
                    <td>
                        <input id="totalCost" readonly="readonly" class="span2" name="totalCost" type="text"
                               value="0.00" size="30" maxlength="50"/>
                        (元)
                    </td>
                </tr>
                <tr>
                    <td>联系电话:</td>
                    <td>
                        <input id="phone" name="phone" type="text" value="${order.phone}" size="30" maxlength="80"/>
                    </td>
                </tr>
                <tr>
                    <td>收货部门:</td>
                    <td>
                        <span class="tBlue">${USER.department.name}</span>
                        <input id="department" name="department.id" type="hidden" value="${USER.department.id}"
                               size="30" maxlength="80"/>
                    </td>
                </tr>
                <tr>
                    <td>联系人:</td>
                    <td>
                        <span class="tBlue">${USER.name }</span>
                        <input id="createUser" name="createUser" type="hidden" value="${USER.name }" size="30"
                               maxlength="80"/>
                    </td>
                </tr>
                <tr>
                    <td valign="top">备注:</td>
                    <td>
                        <textarea id="memo" name="memo" rows="3" class="span6">${order.memo}</textarea>
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                        <a id="myOrder" class="hide" href="${ctx }/adoms/order/myOrderList">我的订单</a>
                        <input id="submitBtn" class="btn span2 btn-primary" disabled="disabled" type="button"
                               value="提交订单"/>
                    </td>
                </tr>
            </table>
        </form>
    </div>
</div>
<script>
    $(document).ready(function () {
        //为inputForm注册validate函数
        $("#inputForm").validate({
            rules: {
                totalCost: {
                    required: true,
                    number: true
                },
                phone: "required",
                receivedTime: "required",
                name: "required",
                orderChargeUser: "required",
                orderCreateTime: "required",
                status: "required",
                "department.id": "required",
                departmentTarget: "required",
                createUser: "required",
                orderNum: "number",
                goodsNumber: {
                    required: true,
                    number: true
                }
            }
        });
        //展示商品详情
        $("#idShow").toggle(function () {
            $("#eyeInfo").show("slow");
            $(this).html("隐藏商品详情");
            $(".icon-eye-open").attr("class", "icon-eye-close");
        }, function () {
            $(".icon-eye-close").attr("class", "icon-eye-open");
            $(this).html("显示商品详情");
            $("#eyeInfo").hide("slow");
        });
        //
        $("#marchandise").change(function () {
            try {
                var mid = $(this).val();
                if (mid) {
                    $("option[value=" + mid + "]", this).hide();
                    $("div", $("#m" + mid)).hide(); //隐藏已经选择的项
                    $("#m" + mid).show("slow").attr("disable", "false");
                    $("#mPrice").html($("[mPrice]", $("#m" + mid)).html());
                    $("#goodsNumber").keyup();
                }
                $("#plz").remove();
                //$("#plz").attr("selected", "selected");
            } catch (e) {
            }
        });
        $("#departmentTarget").change(function () {
            $("#dPlz").remove();
        });
        //计算总价
        $("#goodsNumber").keyup(function () {
            var number = parseInt($(this).val());
            var price = parseFloat($("#mPrice").html());
            var cost = number * price;
            $("#totalCost").val(cost.toFixed(2));
            $(":button").removeAttr("disabled");
        });
        $("a.rclose").click(function () {
            var taget = $($(this).attr("to"));
            taget.hide("slow").attr("disable", "disable");
            clearHistory(taget);
            //显示下拉框的商品选项
            var optionValue = $(this).attr("value");
            $("#marchandise option[value=" + optionValue + "]").show();
            countNum();
            return false;
        });
        $("a.exspan").toggle(function () {
            var taget = $($(this).attr("to"));
            $("div", taget).show("slow");
            return false;
        }, function () {
            var taget = $($(this).attr("to"));
            $("div", taget).hide("slow");
            return false;
        });
        $("div[id^=m]").each(function () {
            var _self = $(this);
            $("input[name=boxNum],input[name=items]", _self).keyup(function () {
                var boxNum = parseInt($("input[name=boxNum]", _self).val());
                var items = parseInt($("input[name=items]", _self).val());
                var numOfBox = parseInt($("[name=numOfBox]", _self).html());
                var total = boxNum * numOfBox + items;
                $("input[name=orderNum]", _self).val(total).keyup();
                countNum();
            });
            $("input[name=orderNum]", _self).keyup(function () {
                var mPrice = parseFloat($("input[name='mPrice']", _self).val());
                var orderNum = parseInt($(this).val());
                $("input[name=totalPrice]", _self).val(mPrice * orderNum);
            });
        });
        $("input[name=orderNum]").on("blur focus change", function () {
            countNum();
        });
        $("#submitBtn").mousedown(function () {
            var len = $("[disable=false]").length;
            if (len == 0 || parseInt($("#goodsNumber").val()) == 0) {
                return false;
            }
            var result = 0;
            //先清理Session中的值
            $.post("${ctx}/adoms/orderLine/clearOlines", function (data) {
                $("[disable=false]").each(function () {
                    var form = $("<form></form>");
                    var parent = $(this).parent();
                    form.append($(this));
                    $.post("${ctx}/adoms/orderLine/addOlines", form.serialize(), function (data) {
                        result++;
                        if (result == len) {
                            $("#inputForm").submit();
                        }
                    }, 'json');
                    parent.append($(this));
                });
            }, 'json');
            return false;
        });
    });
    function clearHistory(oBject) {
        $("input[name=totalPrice]", oBject).val("0.0");
        $("input[name=orderNum]", oBject).val(0);
    }
    function countNum() {
        var prices = $("[disable=false] input[name=totalPrice]");
        var price = 0.0;
        $.each(prices, function (i, item) {
            price += parseFloat($(item).val());
        });
        $("#totalCost").val(price.toFixed(2));
        var orderNums = $("[disable=false] input[name=orderNum]");
        var totalNums = 0.0;
        $.each(orderNums, function (i, item) {
            totalNums += parseInt($(item).val());
        });
        $("#goodsNumber").val(totalNums);
        $(":button").removeAttr("disabled");
    }
</script>
<c:if test="${msg != null}">
    <script>
        msgAlert.show("${msg}");
        $("#myOrder").click();
    </script>
</c:if></body>
</html>